home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / midi / mdlbutls.lha / hr.c < prev    next >
C/C++ Source or Header  |  1988-10-23  |  3KB  |  110 lines

  1. /* hex receive from midi.library */
  2.  
  3. /*
  4.     display incoming MIDI messages in Hex to the console.
  5.  
  6.     Shows proper method of waiting for, receiving, processing, and
  7.     disposing of MidiPackets.
  8. */
  9.  
  10.  
  11. #include <libraries/dos.h>
  12. #include <clib/macros.h>
  13. #include <midi/midi.h>
  14. #include <functions.h>
  15.  
  16. #include <stdio.h>
  17.  
  18. void *MidiBase;
  19.  
  20. struct MDest *dest;
  21. struct MRoute *route;
  22. struct MidiPacket *packet;  /* buffer this in case we get shut down before freeing the current message */
  23.  
  24. main(argc,argv)
  25. char **argv;
  26. {
  27.     char *sname = "MidiIn";
  28.  
  29.     printf ("MIDI Hex Display\n");
  30.  
  31.     if (argc > 1 && *argv[1] == '?') {
  32.     printf ("usage: hr [source]\n");
  33.     exit (1);
  34.     }
  35.  
  36.     if (!(MidiBase = OpenLibrary (MIDINAME,MIDIVERSION))) {
  37.     printf ("can't open midi.library\n");
  38.     goto clean;
  39.     }
  40.  
  41.     if (argc > 1) {         /* if there's an argument, use it as an alt. source name */
  42.     sname = argv[1];
  43.     }
  44.                 /* create out dest node (private) */
  45.     if (!(dest = CreateMDest (NULL,NULL))) {
  46.     printf ("can't create Dest\n");
  47.     goto clean;
  48.     }
  49.                 /* create out route to MidiIn or whatever the user specified */
  50.     if (!(route = MRouteDest (sname, dest, NULL))) {        /* use default route on our MDest (defaults to everything) */
  51.     printf ("can't create Route (can't find source \"%s\"?)\n",sname);
  52.     goto clean;
  53.     }
  54.  
  55.     process(dest);          /* process until shutdown */
  56.  
  57. clean:
  58.     cleanup();
  59. }
  60.  
  61. _abort()                    /* abort routine called when CTRL-C is hit (Aztec) */
  62. {
  63.     fflush(stdout);
  64.     cleanup();
  65.     exit (1);
  66. }
  67.  
  68. cleanup()
  69. {
  70.     if (packet) FreeMidiPacket (packet);
  71.     if (route) DeleteMRoute (route);
  72.     if (dest) DeleteMDest (dest);
  73.     if (MidiBase) CloseLibrary (MidiBase);
  74. }
  75.  
  76. process (dest)
  77. struct MDest *dest;
  78. {
  79.     ULONG flags = SIGBREAKF_CTRL_C | (1L << dest->DestPort->mp_SigBit);
  80.  
  81.     while (!(Wait(flags) & SIGBREAKF_CTRL_C)) {         /* wait until we get a message or CTRL-C is hit, quit on CTRL-C */
  82.     while (packet = GetMidiPacket (dest)) {         /* get a packet */
  83.         dumppacket (packet);                        /* print it */
  84.         FreeMidiPacket (packet);                    /* free it */
  85.     }
  86.     }
  87. }
  88.  
  89.  
  90. dumppacket (packet)
  91. struct MidiPacket *packet;
  92. {
  93.     if (packet->Type == MMF_SYSEX) {                /* if it's a System Exclusive message... */
  94.     dump (packet->MidiMsg,MIN(packet->Length,8));   /* only print the first 8 bytes */
  95.     printf ("... (%d bytes)\n",packet->Length);
  96.     }
  97.     else {                        /* otherwise */
  98.     dump (packet->MidiMsg,packet->Length);          /* print the whole message */
  99.     printf ("\n");
  100.     }
  101. }
  102.  
  103.  
  104. dump (s,len)                                    /* print len bytes in hex */
  105. UBYTE *s;
  106. int len;
  107. {
  108.     while (len--) printf ("%02x ",*s++);
  109. }
  110.